<template>
  <div class="app-container">

    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="发布者昵称: ">
        <el-input v-model="articleQuery.nickname" placeholder="发布者昵称"/>
      </el-form-item>
      <el-form-item label="内容标题: ">
        <el-input v-model="articleQuery.keyWords" placeholder="输入内容标题"/>
      </el-form-item>
      <el-form-item label="类别: ">
        <el-select v-model="articleQuery.articleType" clearable placeholder="输入类别">
          <el-option :value="type.id" :label="type.title" v-for="type in articleTypeList" :key="type.id"/>
        </el-select>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="articleList"
      border
      fit
      :header-cell-style="{background:'#0078AD',color:'#FFFFFF'}"
      highlight-current-row>

      <el-table-column
        label="序号"
        align="center">
        <!-- slot-scope="scope" 属性本质就是一个插槽 -->
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="userId" label="发布者唯一标识" />
      <el-table-column prop="nickname" label="发布者昵称" />
      <el-table-column prop="title" label="标题" :show-overflow-tooltip="true"/>
      <el-table-column prop="description" label="简介" :show-overflow-tooltip="true"/>
      <el-table-column prop="content" label="内容" :show-overflow-tooltip="true"/>
      <el-table-column prop="viewCount" label="浏览数" />
      <el-table-column prop="gmtModified" label="发布时间" />
      
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />


  </div>
</template>

<script>
import article from '@/api/edu/article'

export default {
    data() {
        return {
          articleTypeList: [],
          articleList:null,//查询之后接口返回集合
          page:1,//当前页
          limit:5,//每页记录数
          total:0,//总记录数
          articleQuery:{} //条件封装对象
        }
    },
    created() {
        this.getList()
        this.getArticleTypeList()
    },
    methods: {
        getArticleTypeList() {
            article.getArticleTypeList().then(response => {
                this.articleTypeList = response.data.blogTypeList
            })
        },

        getList(page = 1) {
            this.page = page
            article.pageArticleCondition(this.page, this.limit, this.articleQuery)
                .then(response => {
                    this.articleList = response.data.rows
                    this.total = response.data.total
                })
                .catch(error => {
                    console.log(error)
                })
        },
        //清空条件查询内容
        resetData() {
            //清空内容
            this.articleQuery = {}
            //重新查询列表
            this.getList()
        },
        //删除
        removeDataById(id) {
            this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            //发起请求
            article.deleteArticleById(id)
                .then(response => {
                    this.getList((this.total % this.limit == 1 && this.page > 1 ? this.page - 1 : this.page))
                    this.$message({
                    type: 'success',
                    message: '删除成功!'
                  });
                })
          }).catch((response) => { // 失败
            if (response === 'cancel') {
              this.$message({
                type: 'info',
                message: '已取消删除'
              })
            }
          });
        },
    },
}

</script>
